package com.pig.graduate.controller.sys;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig.graduate.common.constant.Constant;
import com.pig.graduate.domain.sys.SysBuildings;
import com.pig.graduate.domain.sys.SysColumn;
import com.pig.graduate.domain.sys.SysMenu;
import com.pig.graduate.domain.sys.SysRole;
import com.pig.graduate.result.MyResult;
import com.pig.graduate.service.sys.SysBuildingsService;
import com.pig.graduate.service.sys.SysColumnService;
import com.pig.graduate.util.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.jsf.FacesContextUtils;
import reactor.core.publisher.Flux;

import javax.validation.Valid;
import java.awt.print.PrinterGraphics;
import java.util.*;

@Api(tags = {"栋舍管理"}, description = "栋舍管理")
@RestController
@RequestMapping("/sys/buildings")
public class SysBuildingsController {

    @Autowired
    private SysBuildingsService sysBuildingsService;

    @Autowired
    private SysColumnService sysColumnService;

    /**
     * 栋舍列表
     * @param sysBuildings
     * @return
     */
    @ApiOperation(value = "栋舍列表",notes = "栋舍列表")
    @PostMapping("/list")
    public MyResult buildingsList(@RequestBody SysBuildings sysBuildings){
        // 查询所有栋舍信息
        Page<SysBuildings> sysBuildingsPage = new Page<>(sysBuildings.getPageNum(),sysBuildings.getPageSize());
        Page<SysBuildings> page = new LambdaQueryChainWrapper<SysBuildings>(sysBuildingsService.getBaseMapper())
                .like(SysBuildings::getName, sysBuildings.getName())
                .like(SysBuildings::getBuildingsType, sysBuildings.getBuildingsType())
                .page(sysBuildingsPage);
        return MyResult.success().put("buildingsList",page);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询",notes = "根据id查询")
    @GetMapping("/{id}")
    public MyResult findById(@PathVariable(value = "id")Long id){
        SysBuildings sysBuildings = sysBuildingsService.getById(id);
        return MyResult.success(sysBuildings);
    }

    /**
     * 添加或者修改
     * @param sysBuildings
     * @return
     */
    @ApiOperation(value = "添加或者修改",notes = "添加或者修改")
    @PostMapping("/saveAndUpdate")
    public MyResult saveAndUpdate(@RequestBody @Valid SysBuildings sysBuildings){

        if(sysBuildings.getId()==null || sysBuildings.getId()==0){
            sysBuildings.setCreateTime(new Date());
            sysBuildingsService.save(sysBuildings);
            List<SysColumn> list = new ArrayList<>();
            for(int i = 0; i < sysBuildings.getSumColumnNumber(); i++){
                SysColumn sysColumn = new SysColumn();
                sysColumn.setBuildingsId(sysBuildings.getId());
                sysColumn.setCreateTime(new Date());
                if(i < 10){
                    sysColumn.setName("0"+i);
                }else{
                    sysColumn.setName(String.valueOf(i));
                }
                list.add(sysColumn);
            }
            sysColumnService.saveBatch(list);
        }else{
            sysBuildings.setUpdateTime(new Date());
            sysBuildingsService.updateById(sysBuildings);
        }

        return MyResult.success();
    }



    /**
     * 删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除",notes = "删除")
    @PostMapping("/delete")
    public MyResult delete(@RequestBody List<Long> ids){
        sysBuildingsService.removeByIds(ids);
        return MyResult.success();
    }
}
